<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import HeaderNavTabarVue from '@/components/HeaderNavTabar/HeaderNavTabar.vue'
import moren from '@/static/icon/tongxunlu/moren.png'
import { getDeptTree } from '@/api/dep'
import store from '@/store'
import { getUserProfile } from '@/api/system/user'

const cardData = ref([])
const typeMarker = ref(null)
const isDanXuan = ref(null)
const noSelectSelf = ref(null)
const userId = ref(null)
onLoad(async (v) => {
  console.log(v)
  getUserProfile().then((res) => {
    userId.value = res.data.userId
  })
  let depId = decodeURIComponent(v.depId)
  if (v.typeMarker) {
    typeMarker.value = decodeURIComponent(v.typeMarker)
  }
  if (v.isDanXuan) {
    isDanXuan.value = decodeURIComponent(v.isDanXuan)
  }
  if (v.noSelectSelf) {
    noSelectSelf.value = decodeURIComponent(v.noSelectSelf)
  }

  let params = {
    pageNum: 1,
    pageSize: 10000,
    deptId: depId,
  }
  let { code, rows } = await getDeptTree(params)
  if (code === 200) {
    cardData.value = rows
  }
})

const getPerson = (item) => {
  if (typeMarker.value) {
    // 人事--离职--离职申请人
    store._mutations.setOptionPerson2[0](item)
    uni.$emit('getOptionPerson2', item)
  } else {
    store._mutations.setOptionPerson[0](item)
    uni.$emit('getOptionPerson', item)
  }

  uni.navigateBack({
    delta: 2,
  })
}

const viewTitle = ref('通讯录')
// const baseUrl = config.baseUrl
const baseUrl = 'http://123.53.123.80:33000/prod-api'

const optionListData = ref([])
const checkboxChange = (e) => {
  optionListData.value = e.detail.value
}
const scroll = (v) => {
  console.log(v)
}

const handleClick = () => {
  if (Number(isDanXuan.value) === 1 && optionListData.value.length > 1) {
    // 单选 isDanXuan 1
    uni.showToast({
      title: '只能选择一个人员',
      icon: 'none',
    })
    return
  }

  // 新建日程不能选择自己
  const flag = optionListData.value.some((item) => item.userId === userId.value)
  if (Number(noSelectSelf.value) === 1 && flag) {
    uni.showToast({
      title: '不能选择自己',
      icon: 'none',
    })
    return
  }

  getPerson(optionListData.value)
}
</script>

<template>
  <HeaderNavTabarVue :iconFont="'icon-cuowuguanbiquxiao'" :title="viewTitle" />
  <view class="uv-empty" v-if="cardData.length === 0">
    <uv-empty icon="../../../static/images/empty.png"></uv-empty>
  </view>
  <view v-else class="detail-box">
    <checkbox-group @change="checkboxChange">
      <label
        v-for="item in cardData"
        :key="item.value"
        class="uni-list-cell uni-list-cell-pd msg-list"
      >
        <view>
          <checkbox :checked="item.checked" :value="item" />
        </view>
        <view class="list-left">
          <view class="imageWrapper">
            <image v-if="item.avatar" :src="baseUrl + item.avatar"></image>
            <image v-else :src="moren"></image>
          </view>
          <view class="list-right">
            <view class="msg-type-author">
              <h4>{{ item.nickName }}</h4>
            </view>
            <text>{{ item.phonenumber }}</text>
          </view>
        </view>
      </label>
    </checkbox-group>
  </view>
  <view class="buttonContainer" v-if="optionListData.length > 0">
    <scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll">
      <image
        v-for="(item, index) in optionListData"
        :key="index"
        :src="item.avatar ? baseUrl + item.avatar : moren"
        style="width: 80rpx; height: 80rpx; margin-right: 10rpx"
      />
    </scroll-view>
    <button class="buttonClass" @tap="handleClick">确定({{ optionListData.length }})</button>
  </view>
</template>

<style lang="scss" scoped>
@import '@/static/style/index.scss';

:deep(.uni-scroll-view) {
  white-space: nowrap;

  .uni-scroll-view-content {
    width: 65%;
    overflow: auto;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    //@include flex($align: center);
    height: 130rpx;
    padding: 20rpx;
    box-sizing: border-box;
    background-color: #f7f8fa;
  }
}

.detail-box {
  .time-box {
    margin: 32rpx 24rpx;
    color: #86909c;
    font-size: 24rpx;
    font-style: normal;
    font-weight: 400;
  }

  :deep(.uni-checkbox-input) {
    border-radius: 50%;
    width: 40rpx !important;
    height: 40rpx !important;
  }

  .msg-list {
    @include flex($align: center);
    background-color: #fff;
    height: 108rpx;
    padding: 0 $spacing-row-base;

    &:active {
      background-color: #eff0f2;
    }

    .list-left {
      @include flex($align: center);
      margin-left: 20rpx;

      .imageWrapper {
        @include flex($align: center, $justify: space-between);
        background-color: #e8f3ff;
        padding: 10rpx;
        margin-right: 16rpx;
        border-radius: 32rpx;
      }

      image {
        width: 42rpx;
        height: 42rpx;
        border-radius: 50%;
      }

      .msg-type-author {
        @include flex($align: center);

        h4 {
          margin-right: 16rpx;
          color: #000000;
          font-family: 'PingFang SC';
          font-size: 32rpx;
          font-style: normal;
          font-weight: 400;
          line-height: 44.8rpx;
        }

        p {
          text-align: center;
          font-family: 'PingFang SC';
          font-size: 20rpx;
          font-style: normal;
          font-weight: 500;
          padding: 3rpx 8rpx;
          border-radius: 4rpx;
        }
      }

      text {
        align-self: stretch;
        color: #86909c;
        font-family: 'PingFang SC';
        font-size: 24rpx;
        font-style: normal;
        font-weight: 400;
        line-height: 33.6rpx;
        margin-top: 8rpx;
      }
    }

    .time-txt {
      color: #86909c;
      text-align: right;
      font-family: 'PingFang SC';
      font-size: 28rpx;
      font-style: normal;
      font-weight: 400;
      line-height: 39.2rpx;
    }
  }
}

.buttonContainer {
  position: fixed;
  bottom: 0;
  right: 0;
  z-index: 999;
  height: 130rpx;
  width: 100%;
  background-color: #f7f8fa;
}

.buttonClass {
  width: 30%;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  color: #fff;
  background: #165dff;
  position: fixed;
  bottom: 30rpx;
  right: 2.5%;
  z-index: 999;
}
</style>
